package com.robusta.cityuate.services;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpRequestBase;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;

import com.robusta.cityuate.objects.Offering;
import com.robusta.cityuate.utils.ConnectionUtils;

public class DeleteOfferingService extends HTTPService {

	private static final String SPOTS_URL = "/spots/";
	private static final String OFFERING_URL = "/offerings/";
	private Context mContext = this;

	@Override
	protected void onResponse() {
		Intent deleteSpotUIupdate = new Intent(assignedFilter);
		switch (responseStatusCode) {
		case HttpStatus.SC_NO_CONTENT: {
			// remove connection from shared pref
			ConnectionUtils.setDisconnecting(mContext, true, assignedFilter,
					null);
			// update UI if active
			deleteSpotUIupdate.putExtra(RESUlT, true);
			sendOrderedBroadcast(deleteSpotUIupdate, null);
			return;
		}
		case HttpStatus.SC_NOT_FOUND:
		case HttpStatus.SC_UNAUTHORIZED:
			JSONObject msg;
			try {
				msg = new JSONObject(responseString);
				onNoResponse(msg.getString(HTTPService.MESSAGE));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onNoResponse(String msg) {
		// remove connection from shared pref keeping result and reason
		ConnectionUtils.setDisconnecting(mContext, false, assignedFilter, msg);
		// update UI if active
		Intent deleteSpotUIupdate = new Intent(assignedFilter);
		deleteSpotUIupdate.putExtra(RESUlT, false);
		sendOrderedBroadcast(deleteSpotUIupdate, null);
	}

	@Override
	protected HttpRequestBase initRequest() {
		Offering offer = (Offering) extras.getSerializable(Offering.OFFERING);
		return new HttpDelete(API_URL + SPOTS_URL + offer.getSpot().getId()
				+ OFFERING_URL + offer.getId());
	}

}
